微软语音合成(tts)服务申请和调用

您所在的位置:网站首页 azure 免费 key 微软语音合成(tts)服务申请和调用

微软语音合成(tts)服务申请和调用

2024-07-10 17:29:30| 来源: 网络整理| 查看: 265

1、申请账户:

https://azure.microsoft.com/zh-cn/free/

在这里插入图片描述 这里有个视频教程,根据此完成申请流程: https://www.bilibili.com/video/BV15a4y1W7re?vd_source=bf07f28d37849885d215dc3aea189eba 申请完成后,就可以到这里申请资源: https://portal.azure.com/#home

点击资源组,里面就有部署好的服务了 在这里插入图片描述 在这里插入图片描述 点击这里,可以获取 subscription_key,另外还有个就是位置service_region (上图就是east asia),这两个后面会用到。

2、调用服务

在完成微软azure服务账号申请后,就可以进行调用了。代码:

''' After you've set your subscription key, run this application from your working directory with this command: python TTSSample.py ''' import os, requests, time from xml.etree import ElementTree # This code is required for Python 2.7 try: input = raw_input except NameError: pass ''' If you prefer, you can hardcode your subscription key as a string and remove the provided conditional statement. However, we do recommend using environment variables to secure your subscription keys. The environment variable is set to SPEECH_SERVICE_KEY in our sample. For example: subscription_key = "Your-Key-Goes-Here" ''' if 'SPEECH_SERVICE_KEY' in os.environ: subscription_key = os.environ['SPEECH_SERVICE_KEY'] else: print('Environment variable for your subscription key is not set.') exit() class TextToSpeech(object): def __init__(self, subscription_key): self.subscription_key = subscription_key self.tts = input("What would you like to convert to speech: ") self.timestr = time.strftime("%Y%m%d-%H%M") self.access_token = None ''' The TTS endpoint requires an access token. This method exchanges your subscription key for an access token that is valid for ten minutes. ''' def get_token(self): fetch_token_url = "https://westus.api.cognitive.microsoft.com/sts/v1.0/issueToken" headers = { 'Ocp-Apim-Subscription-Key': self.subscription_key } response = requests.post(fetch_token_url, headers=headers) self.access_token = str(response.text) def save_audio(self): base_url = 'https://westus.tts.speech.microsoft.com/' path = 'cognitiveservices/v1' constructed_url = base_url + path headers = { 'Authorization': 'Bearer ' + self.access_token, 'Content-Type': 'application/ssml+xml', 'X-Microsoft-OutputFormat': 'riff-24khz-16bit-mono-pcm', 'User-Agent': 'YOUR_RESOURCE_NAME' } xml_body = ElementTree.Element('speak', version='1.0') xml_body.set('{http://www.w3.org/XML/1998/namespace}lang', 'en-us') voice = ElementTree.SubElement(xml_body, 'voice') voice.set('{http://www.w3.org/XML/1998/namespace}lang', 'en-US') voice.set('name', 'en-US-Guy24kRUS') # Short name for 'Microsoft Server Speech Text to Speech Voice (en-US, Guy24KRUS)' voice.text = self.tts body = ElementTree.tostring(xml_body) response = requests.post(constructed_url, headers=headers, data=body) ''' If a success response is returned, then the binary audio is written to file in your working directory. It is prefaced by sample and includes the date. ''' if response.status_code == 200: with open('sample-' + self.timestr + '.wav', 'wb') as audio: audio.write(response.content) print("\nStatus code: " + str(response.status_code) + "\nYour TTS is ready for playback.\n") else: print("\nStatus code: " + str(response.status_code) + "\nSomething went wrong. Check your subscription key and headers.\n") print("Reason: " + str(response.reason) + "\n") def get_voices_list(self): base_url = 'https://westus.tts.speech.microsoft.com/' path = 'cognitiveservices/voices/list' constructed_url = base_url + path headers = { 'Authorization': 'Bearer ' + self.access_token, } response = requests.get(constructed_url, headers=headers) if response.status_code == 200: print("\nAvailable voices: \n" + response.text) else: print("\nStatus code: " + str(response.status_code) + "\nSomething went wrong. Check your subscription key and headers.\n") if __name__ == "__main__": app = TextToSpeech(subscription_key) app.get_token() app.save_audio() # Get a list of voices https://docs.microsoft.com/en-us/azure/cognitive-services/speech-service/rest-text-to-speech#get-a-list-of-voices # app.get_voices_list()

参考文档: https://docs.microsoft.com/zh-cn/azure/cognitive-services/speech-service/ https://github.com/Azure-Samples/Cognitive-Speech-TTS/blob/28681c8292c95aebb36d3696b8822b4cd17c3c45/Samples-Http/OLD/Python/TTSSample.py



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭